<?php

namespace App\Http\Controllers\Admin\Assistant;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\UserRepositoryEloquent;
use App\Repositories\MemberRepositoryEloquent;

use App\User;
use App\Models\Commission;
use App\Models\ChatMessage;
use Breadcrumbs, Toastr,Config,Validator,DB;
use Illuminate\Http\Request;
use App\Models\FinancialDetails;

class ChatController extends BaseController{

    protected $user;

    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;


    }

    public function index(Request $request){
        $data=['type'=>-1,'name'=>'','mobile'=>'','from'=>'','to'=>''];
        $where=[];
        $whereHas=[];
        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='mobile') && $v!=''){
                    $whereHas[] = ["to",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
                
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }


        $smses = ChatMessage::where($where)
                    ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                    })
                    ->orderBy('id','desc')->paginate(20);
        //dd($smses);
        return view('admin.assistant.chat',compact('smses','data'));
    }

    public function destroy($id)
    {
        $article = ChatMessage::find($id);
        $result = $article->delete();
        return response()->json($result ? ['status' => 1] : ['status' => 0]);
    }

    public function gagUsers(Request $request)
    {   
        $where = [];
        $where[] = ['gag','=',1];
        $whereHas = [];
        if($request->has('mobile') && !empty($request['mobile'])){
            $where[] = ['mobile','like','%'.$request['mobile'].'%'];
        }
        if($request->has('name') && !empty($request['name'])){
            $where[] = ['name','like','%'.$request['name'].'%'];
        }
        if($request->has('roleid') && !empty($request['roleid'])){
            $whereHas[] = ['role_id','=',$request['roleid']];

            $users = $this->user
                ->where($where)
                ->whereHas('roles',function($query) use($whereHas){
                            $query->where($whereHas);
                })->orderBy('balance','desc')->paginate(10);
        }else{
            $users = $this->user
                ->where($where)
                ->orderBy('balance','desc')->paginate(10);
        }
        
        return view('admin.assistant.gag-users', compact('users'));
    }

    public function gag(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'mobile' => 'required|zh_mobile|max:255'
        ]);
        $mobile = $request->mobile;

        $validator->after(function($validator) use ($mobile){
            if(!$mobile){
                return;
            }
            $user = User::where('mobile','=',$mobile)->first();
            if (!$user) {
                $validator->errors()->add('mobile', '用户不存在!');
                return;
            }
            $user->gag = 1;
            $result = $user->save();
            if(!$result){
                $validator->errors()->add('mobile', '添加失败!');
            }
            

        });

        if ($validator->fails()) {
            return redirect(route('admin.assistant.gag-users'))->withErrors($validator)
                        ->withInput();
        }
        return redirect(route('admin.assistant.gag-users'));
    }

    public function removeGag($id)
    {
        $user = User::find($id);
        $user->gag = 0;
        $result = $user->save();
        return response()->json($result ? ['status' => 1] : ['status' => 0]);
    }

}